package com.ferdict;

import java.io.StringReader;

import javax.servlet.http.HttpServletRequest;

import org.apache.http.HttpRequest;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.sun.xml.internal.ws.transport.http.HttpAdapter;

import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.StreamingResolution;
import net.sourceforge.stripes.action.UrlBinding;

@UrlBinding("/outfits.json")
public class OutfitsActionBean implements ActionBean {
	private ActionBeanContext context;

	// @Validate(required = true)
	// private double numberOne;
	// @Validate(required = true)
	// private double numberTwo;
	// private double result;

	public ActionBeanContext getContext() {
		return context;

	}

	public void setContext(ActionBeanContext context) {
		this.context = context;
	}

	public static final String GET = "GET";
	public static final String POST = "POST";
	public static final String PUT = "PUT";
	public static final String DELETE = "DELETE";

	@DefaultHandler
	public Resolution dispatch() throws JSONException {
		HttpServletRequest req = getContext().getRequest();
		String jsonStr = null;
		if (null != req) {
			if (req.getMethod().equals(GET)) {
				String userIdStr = req.getParameter("userId");
				if (userIdStr == null) {
					JSONArray arr = new JSONArray();
					for (int i = 1; i < 11; i++) {
						JSONObject o = new JSONObject();
						o.put("id", i);
						o.put("name", "name-" + i);
						o.put("photo", req.getScheme() + "://"
								+ req.getServerName() + ":"
								+ req.getServerPort()
								+ "/FerdictService/photos/" + i + ".jpeg");
						o.put("likes", (int) (Math.random() * 100));
						o.put("dislikes", (int) (Math.random() * 100));
						o.put("owner", (int) (Math.random() * 100));
						o.put("share", true);

						arr.put(o);
					}
					jsonStr = arr.toString();
				
				} else {
					JSONArray arr = new JSONArray();
					for (int i = 11; i < 16; i++) {
						JSONObject o = new JSONObject();
						o.put("id", i);
						o.put("name", "name-" + i);
						o.put("photo", req.getScheme() + "://"
								+ req.getServerName() + ":"
								+ req.getServerPort()
								+ "/FerdictService/photos/" + i + ".jpeg");
						o.put("likes", (int) (Math.random() * 100));
						o.put("dislikes", (int) (Math.random() * 100));
						o.put("owner", (int) (Math.random() * 100));
						o.put("share", true);

						arr.put(o);
					}
					jsonStr = arr.toString();
				}
			} else if (req.getMethod().equals(PUT)) {
				String outfitIdStr = req.getParameter("outfitId");
				String likeStr = req.getParameter("like");
				JSONObject o = new JSONObject();
				if ("0".equals(likeStr))
					o.put("likes", 100);
				else
					o.put("dislikes", 101);
				jsonStr = o.toString();
			} else if (req.getMethod().equals(POST)) {
				if (null != req.getParameter("token")) {
					jsonStr = "1";
				}
			}
		}
		return new StreamingResolution("text/javascript", new StringReader(
				jsonStr));
	}
	// public double getNumberOne() {
	// return numberOne;
	// }
	//
	// public void setNumberOne(double numberOne) {
	// this.numberOne = numberOne;
	// }
	//
	// public double getNumberTwo() {
	// return numberTwo;
	// }
	//
	// public void setNumberTwo(double numberTwo) {
	// this.numberTwo = numberTwo;
	// }
	//
	// public double getResult() {
	// return result;
	// }
	//
	// public void setResult(double result) {
	// this.result = result;
	// }

	// /** An event handler method that adds number one to number two. */
	// @DefaultHandler
	// public Resolution addition() {
	// result = numberOne + numberTwo;
	// return new ForwardResolution("/index.jsp");
	// }

	// /** An event handler method that divides number one by number two. */
	// public Resolution division() {
	// result = numberOne / numberTwo;
	// return new StreamingResolution("text/javascript", new StringReader(
	// new JsonBuilder(new Outfit()).build()));
	// // return new StreamingResolution("text", new StringReader("{id:1}"));
	// }
	//
	// /**
	// * An example of a custom validation that checks that division operations
	// * are not dividing by zero.
	// */
	// @ValidationMethod(on = "division")
	// public void avoidDivideByZero(ValidationErrors errors) {
	// if (this.numberTwo == 0) {
	// errors.add("numberTwo", new SimpleError(
	// "Dividing by zero is not allowed."));
	// }
	// }
}
